<?php
class UserDao {
	
	
	private $mysqli;
	
	/*
	 * 连接数据库
	 * @author:cjy
	 */
	public function UserDao(){
		$this->mysqli = new mysqli('10.4.12.173', 'uZavFD5HSjZwz', 'p9KZTHKCUR8Kw','d2eed217eab864fbe960ee093bed9e1e2');
		mysqli_query($this->mysqli,'set names utf8');
	}
	
	/*
	 *  用户注册 
	 *  @username:用户名,@password:密码 ,@email:邮箱,@code验证码
	 *  @return 成功返回1,失败返回0
	 */
	public function regist($username,$password,$email,$code) {
		$this->mysqli->autocommit(false);
		$query = "insert into user(u_username,u_password,u_email,u_evc)
							values('".$username."','".$password."','".$email."','".$code."');";
		
		mysql_query("SET NAMES utf-8");
		$result = $this->mysqli->query($query, MYSQLI_STORE_RESULT);
		if(!$this->mysqli->errno){
			$this->mysqli->commit();
			return 1;
		}else{
			$this->mysqli->rollback();
			return 0;
		}
		
	} 
	
	/*
	 * 查找用户名是否存在
	 * @uername:用户名
	 * @return 成功返回1 失败返回0
	 */
	public function nameIsExist($username){
		$query = "SELECT u_username from user Where u_username = '".$username."';";
		$result = $this->mysqli->query($query, MYSQLI_STORE_RESULT);
		if($result->num_rows == 1)
			return 0;         //用户名已经被占用
		else 
			return 1;        //用户名可以使用
		
	}
	
	
	/*
	 * 用户登陆
	 * @username:用户名,@password：密码
	 * return 密码正确返回1 密码错误返回2 账号不正确返回0
	 */
	public function login($username,$password){
		$query = "SELECT u_password FROM user Where u_username = '".$username."';";
		$result = $this->mysqli->query($query, MYSQLI_STORE_RESULT);
		
		if($result->num_rows){
			$temp = $result->fetch_row();
			$passFromDB = $temp[0];
			if($passFromDB == $password)
			{
				return 1;      //密码正确
			}
				
			else {
				return 2;    //密码不正确
			}
				
		}else 
		{
			return 0;       //账号不存在
		}
			
		
	}
	
	/**
	 * 验证邮箱地址
	 * @param  $code 邮箱验证码
	 */
	public function validationEmail($code) {
		$query = "SELECT u_username FROM user WHERE u_evc = '".$code."';";
		$result = $this->mysqli->query($query, MYSQLI_STORE_RESULT);
		if($result->num_rows) {
			if($this->emailCodeIsTrue($code) === 1)
				return 1;
			else 
				return 0;
		}else {
			return 0;     
		}
	}
	
	/**
	 * 邮箱验证完成
	 * @param unknown $code
	 */
	public function emailCodeIsTrue($code) {
		$this->mysqli->autocommit(false);
		$query = "UPDATE user SET u_isVa=1 WHERE u_evc = '".$code."';";
		$result = $this->mysqli->query($query, MYSQLI_STORE_RESULT);
		if(!$this->mysqli->errno){
			$this->mysqli->commit();
			return 1;
		}else{
			$this->mysqli->rollback();
			return 0;
		}
	}
	
	
	/**
	 * 登录时判断邮箱是否验证
	 * @param u_username 用户名
	 */
	public function emailIsValidation($u_username) {
		$query = "SELECT u_isVa FROM user WHERE u_username = '".$u_username."';";
		$result = $this->mysqli->query($query, MYSQLI_STORE_RESULT);
		$temp = $result->fetch_row();
		if($temp[0]) {
			return 1;         //邮箱已经验证
		}else {
			return 0;        //邮箱未验证
		}
			
		
	}
	
	
	/**
	 * 根据用户名查找id
	 * @param unknown $username 用户名
	 */
	public function selectUserIDByUsername($username) {
		$query = "SELECT u_id FROM user WHERE u_username = '".$username."';";
		$result = $this->mysqli->query($query, MYSQLI_STORE_RESULT);
		$temp = $result->fetch_row();
		return $temp[0];
	}
	
	
	
}

?>